/*
 * list.c
 *
 *  Created on: 2021年6月13日
 *      Author: limao
 */

#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
#include <stdio.h>
#include <stdarg.h>

static volatile unsigned int NUMBER_OF_LIST=0;

list_error  _LIST_PROCESS_SUCESSFUL  = 0;
list_error  _LIST_MEMORY_FAIL        = 1;
list_error  _LIST_NODE_INSERT_FAIL   = 2;
list_error  _LIST_NODE_DELET_FAIL    = 3;

// 创建链表
struct list *InitList(void *pdata) {

	struct list *newlist = (struct list*) malloc(
			sizeof(struct list));

	if (newlist == NULL) {
		return NULL;
	}

	struct list_node *newnode = (struct list_node*) malloc(
			sizeof(struct list_node));

	if (newnode == NULL) {
		return NULL;
	}

		newnode->per = newnode->next = NULL;
		newnode->data = pdata;

		newlist->head=newlist->tail=newnode;
		newlist->len=1;

	if(NUMBER_OF_LIST<=LIST_MAX_NUMBER)
		NUMBER_OF_LIST++;
	else
		return NULL;

	return newlist;
}


// 删除链表 释放所有内存
void DeinitList(struct list *self){
	  for(int i = self->len-1 ; i>0 ; i--)
	  {
		  list_delet_node(self , 0);
		  printf("asdf \n");
	  }
	  free(self);
	  self=NULL;
}


// node 符号为-1,在列表末尾插入 ， node为0 在表头插入 ， node >=0 在对应位置插入
list_error list_insert_node(struct list *self, int insert_index , void *pdata) {

	struct list_node *newnode = (struct list_node*) malloc(
			sizeof(struct list_node));

	if (newnode == NULL) {
		return _LIST_MEMORY_FAIL;
	}

  		newnode->per = newnode->next = NULL;
		newnode->data = pdata;


		if(insert_index == 0)//insert at head
		{
			self->head->per=newnode;
			newnode->next=self->head;
			self->head=newnode;
		}
		else if(insert_index == -1)//insert at tail
		{

			self->tail->next=newnode;
			newnode->per=self->tail;
			self->tail=newnode;

		}
		else{
			if(insert_index>0&&insert_index<=self->len)
			{
				struct list_node *index_node;
				for(int len=insert_index; len>0 ;len--)
				{
					index_node=self->head->next;
				}
				index_node->per->next=newnode;
				newnode->per=index_node->per;
				index_node->per=newnode;
				newnode->next=index_node;
			}
			else
				return _LIST_NODE_INSERT_FAIL;
		}

		self->len++;

	return _LIST_PROCESS_SUCESSFUL;

}


// node 符号为-1,在列表末尾删除 ， node为0 在表头删除 ， node >=0 在对应位置删除
list_error list_delet_node(struct list *self, int insert_index) {

	struct list_node *p;

		if(insert_index == 0)//delet at head
		{
			p=self->head;
			free(self->head);
			self->head=p->next;
		}
		else if(insert_index == -1)//delet at tail
		{
			p=self->tail;
			free(self->tail);
			self->tail=p->per;
		}
		else{
			if(insert_index>0&&insert_index<=self->len)
			{
				struct list_node *index_node;
				for(int len=insert_index; len>0 ;len--)
				{
					index_node=self->head->next;
				}
				index_node->per->next = index_node->next;
				index_node->next->per = index_node->per;
				free(index_node);
				index_node=NULL;
			}
			else
				return _LIST_NODE_DELET_FAIL;
		}

		self->len--;

	return _LIST_PROCESS_SUCESSFUL;

}

void *get_node_data(struct list *self, unsigned int node){

	volatile struct list_node *p;

	return p->data;
}

int search_data_node(struct list *self, unsigned int node , void *pdata){

	volatile struct list_node *p;

	return self->len;
}

unsigned int get_list_number(void){

	return NUMBER_OF_LIST;
}

unsigned int get_node_number(struct list *self){

	return self->len;
}
